package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.TempOrder;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 临时订单
 */
@Component
public class TempOrderDao implements BaseDao<TempOrder> {
    @Override
    public int insert(TempOrder entity) {

        return DBHelper.executeUpdate(
            "insert into temp_order(name,phone,remark,room_type_id,room_number,in_date,out_date,total_price,temp_order) values(?,?,?,?,?,?,?,?,?)",
            entity.getName(), entity.getPhone(), entity.getRemark(), entity.getRoom_type_id(), entity.getRoom_number(),
            entity.getIn_date(), entity.getOut_date(), entity.getTotal_price(), entity.getTemp_order());
    }

    @Override
    public int update(TempOrder entity) {
        return 0;
    }

    @Override
    public int delete(TempOrder entity) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        return null;
    }

    @Override
    public Map<String, Object> findOne(TempOrder entity) {
        return null;
    }

    @Override
    public int count(Object... obj) {
        return 0;
    }

    // 查询本订单信息
    public Map<String, Object> findTempOrder(Object... obj) {
        return DBHelper.executeQuery("select * from temp_order where temp_order=?", obj).get(0);
    }

    // 查询本订单可用房间
    public List<Map<String, Object>> findTempOrderNullRoom(Object... obj) {
        return DBHelper.executeQuery("select r.id from room r INNER JOIN room_type rt on r.room_type_id=rt.id \n"
            + "INNER JOIN room_status rs on rs.room_id=r.id \n"
            + "INNER JOIN room_status_type rst on rs.status_id=rst.id  where rs.status_id=1 and rt.id=?", obj);
    }

    public void updateTempOrder(String temporder) {
        DBHelper.executeUpdate(
            "update temp_order set predict_order_id=(select MAX(id) from room_predict_order) where temp_order=? ",
            temporder);
    }

    public Map<String, Object> findPredictOrderId(String order) {
        return DBHelper
            .executeQuery("select predict_order_id,room_number,phone from temp_order where temp_order=?", order).get(0);
    }

    public Map<String, Object> findId(String id) {
        String sql =
            "select rpo.ordernumber ordernumber,rt.name tname,rpo.predict_in_time in_date,rpo.linkman linkman,rpo.phone phone"
                + " from room_predict_order rpo "
                + " INNER JOIN room_predict_order_details rpod on rpod.room_predict_order_id=rpo.id  "
                + " INNER JOIN room_type rt on rpod.room_type_id=rt.id where rpo.id=?";
        return DBHelper.executeQuery(sql, id).get(0);
    }

}
